/*
 * Copyright (C), 2014-2015, 联创车盟汽车服务有限公司
 * FileName: CommodityController.java
 * Author:   Freeman
 * Date:     2015年2月2日 上午9:38:40
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>    <time>    <version>    <desc>
 * 修改人姓名        修改时间        版本号        描述
 */
package com.linkage.controller.commodity;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.linkage.interceptor.NeedLogin;
import com.linkage.pojo.commodity.Book;
import com.linkage.pojo.commodity.Category;
import com.linkage.pojo.commodity.Comment;
import com.linkage.pojo.commodity.SpecialBook;
import com.linkage.service.commodity.BookService;
import com.linkage.service.commodity.CategoryService;
import com.linkage.service.commodity.CommentService;
import com.linkage.service.commodity.SpecialBookService;


/**
 * controller功能跳转   整个商品模块的功能跳转
 * 〈功能详细描述〉
 *
 * @author Freeman
 *
 */
@Controller
//注意一些细节，此URl即使没有路劲，也需要用/来设置，那下面说明都需要以/开头,而其他部分也是    注意细节
@RequestMapping("/")
public class CommodityController {
    @Autowired
    private BookService bookService;
    @Autowired
    private SpecialBookService specialBookService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private CommentService commentService;
    
    //展示用户选取的所有类目下的图书
    @RequestMapping(value = "/filter/{categoryId}", method = RequestMethod.GET)
    public String listCategotyBook(@PathVariable("categoryId")Integer categoryId,Model model) {
        List<Book> list=this.bookService.listCategoryBooks(categoryId);
        model.addAttribute("books", list);
        List<Category> list2=this.categoryService.listAllCategory();
        model.addAttribute("category", list2);
        return "commodity/listBook";
    }
    // 显示所有类目
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model) {
        List<Category> list=this.categoryService.listAllCategory();
        model.addAttribute("category", list);
        /*List<SpecialBook> list1=this.specialBookService.listSpecialBooks();
        model.addAttribute("specialBooks",list1);*/
        List<Book> list3=this.bookService.listAllAvailableBook();
        model.addAttribute("books", list3);
        return "commodity/listBook";
    }

    //页面显示用户选取的特定图书的信息    过时
    @RequestMapping(value="/book/{bookId}", method = RequestMethod.GET)
    public String listCategoryBook(@PathVariable("bookId")Integer bookId,Model model){
        Book book=this.bookService.listSelectedBook(bookId);
        model.addAttribute("book", book);
        List<Category> list=this.categoryService.listAllCategory();
        model.addAttribute("category", list);
        return "commodity/book";
    }
    
    //显示页面选取的特定图书的信息
    @RequestMapping(value="/book1/{bookId}",method=RequestMethod.GET)
    public String listBook(@PathVariable("bookId")Integer bookId,Model model){
        List<Map<String,Object>> map=this.bookService.listSelectedBook1(bookId);
        model.addAttribute("list", map);
        List<Category> list=this.categoryService.listAllCategory();
        model.addAttribute("category", list);
        List<Comment> list1=this.commentService.listComments(bookId);
        model.addAttribute("comments", list1);
        return "commodity/book";
    }
    
    //页面显示所有上架图书列表
    @RequestMapping(value="/showallbooks",method= RequestMethod.GET)
    public String listAllAvailableBook(Model model){
        //修改代码时发现，所有跳转到listBook表的都需要
        List<Book> list2=this.bookService.listAllAvailableBook();
        model.addAttribute("books", list2);
        List<Category> list3=this.categoryService.listAllCategory();
        model.addAttribute("category", list3);
        return "commodity/listBook";
    }
   //页面显示所有特价图书            过时
   @RequestMapping(value="/showallspecials",method= RequestMethod.GET)
   public String showallspecials(Model model){
       List<SpecialBook> list=this.specialBookService.listSpecialBooks();
       model.addAttribute("list",list);
       return "commodity/specialBook";
   }
   //显示 级联book表的所有特价图书
   @RequestMapping(value="/showBookSpecials",method=RequestMethod.GET)
   public String showBookSpecials(Model model){
       List<Map<String,Object>> list=this.specialBookService.listBookSpecial();
       model.addAttribute("map", list);
       List<Category> list1=this.categoryService.listAllCategory();
       model.addAttribute("category", list1);
       return "commodity/specialBook";
   }
   
   //页面返回用户搜索到的图书列表
   @RequestMapping(value="/search",method = RequestMethod.POST)
   public String search(@RequestParam("categoryId")Integer categoryId,@RequestParam("bookName")String bookName,Model model){
//       System.out.println(categoryId+"***"+bookName);
       List<Book> list=null;//这个null用的好
       if(categoryId==0){
           //TODO
           if(bookName!=null && bookName.trim()!=""){
               list=this.bookService.listAvailableBook(bookName);
           }
       }else{
           list=this.bookService.search(categoryId, bookName);           
       }
       model.addAttribute("search", list);
       List<Category> list3=this.categoryService.listAllCategory();
       model.addAttribute("category", list3);
       return "commodity/listBook";
   }
}
